<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <!-- <template #title>
          <div>我的频道</div>
      </template> -->
      <van-button
        type="danger"
        plain
        size="mini"
        round
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="8" class="recommend-grid">
      <van-grid-item
        v-for="(value, index) in myChannels"
        :key="value.id"
        class="channel-item"
        @click="onMyChannelClick(value, index)"
      >
        <van-icon
          slot="icon"
          name="close"
          v-show="isEdit && !fixChannel.includes(value.id)"
        ></van-icon>
        <span class="text" :class="{ active: active === index }" slot="text">
          {{ value.name }}
        </span>
      </van-grid-item>
    </van-grid>

    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="8" class="recommend-grid">
      <van-grid-item
        v-for="(value, index) in recommandChannels"
        :key="index"
        class="channel-item"
        icon="plus"
        :text="value.name"
        @click="onAddChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  addUserChannel,
  deleteUserChannel,
  getAllChannels,
} from "@/api/channel";
import { setItem } from "@/utils/storage";
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
    chennelEditShow: {
      type: Boolean,
    },
  },
  data() {
    return {
      isEdit: false, //控制编辑状态显示
      fixChannel: [0], // 固定频道 不允许删除
      channelList: [], //所有频道
    };
  },
  computed: {
    //  计算属性会观测内部依赖数据的变化
    //  如果依赖的数据发生改变 则计算属性会重新执行
    recommandChannels() {
      //通过筛选出来未选中的频道
      //channelList 所有频道
      //myChannels 已选中的频道
      return this.channelList.filter((item) => {
        var flag = this.myChannels.some((item1) => {
          return item.id == item1.id;
        });
        return !flag;
      });
    },
    
  },
  watch: {},
  created() {
    this.loadAllChannels();
  },
  mounted() {},
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.channelList = data.data.channels;
      } catch (err) {
        console.log(err);
        this.$$toast("出错了");
      }
    },
    async onAddChannel(channel) {
      this.myChannels.push(channel);
      //持久化
      if (this.user) {
        //登录
        await addUserChannel([channel.id, this.myChannels.length]);
      } else {
        //未登录
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
    onMyChannelClick(value, index) {
      if (this.isEdit) {
        // 1.如果是固定频道 则不删除
        if (this.fixChannel.includes(value.id)) {
          return;
        }
        // 2.删除频道项
        this.myChannels.splice(index, 1);
        // 3.如果删除的激活频道之前的频道 则更新激活的频道项
        if (index <= this.active) {
          this.$emit("update-active", this.active - 1, true);
        }
        //处理持久化
        this.deleteChannel(value);
      } else {
        //把index 的值传给父组件的active赋值
        this.$emit("update:active", index);
        this.$emit("update:chennelEditShow", false);
      }
    },
    async deleteChannel(channel) {
      try {
        if (this.user) {
          //已登录 则将数据更新到线上
          await deleteUserChannel(channel.id);
        } else {
          //未登录 将数据更新到本地
          setItem("TOUTIAO_CHANNELS", this.myChannels);
        }
      } catch (err) {
        this.$toast("操作失败,请稍后重试");
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    color: #222;
    font-size: 28px;
  }
  /deep/ .recommend-grid {
    .van-grid-item__content {
      background-color: #f5f5f6;

      flex-direction: row;
      color: #222;
      .van-icon {
        font-size: 24px;
      }

      .van-grid-item__text,
      .text {
        font-size: 26px;
        margin-top: 0;
        color: #222;
        white-space: nowrap;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
  }
}
</style>
